package com.hp.demo.vteam.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.hp.demo.vteam.bean.AccountGroup;
import com.hp.demo.vteam.bean.AccountManager;

public class AccountManagerServlet extends BaseServlet {
	private static final long serialVersionUID = -393778548946444384L;

	@Override
	public void edit(HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		String id = request.getParameter("id");
        String name = request.getParameter("name");
		String gender = request.getParameter("gender");
		String workEmail = request.getParameter("workEmail");
		String tel = request.getParameter("tel");
		String mobile = request.getParameter("mobile");
        String desc = request.getParameter("desc");
        String accountGroupId = request.getParameter("accountGroupId");
        
    	Key accountGroupKey = KeyFactory.createKey(AccountGroup.class.getSimpleName(), Long.parseLong(accountGroupId));
        Date date = new Date();
        AccountManager accountManager = null;
        if(id!=null && !id.isEmpty()){
        	accountManager = pm.getObjectById(AccountManager.class, Long.parseLong(id));
        	accountManager.setName(name);
        	accountManager.setGender(gender);
        	accountManager.setTel(tel);
        	accountManager.setWorkEmail(workEmail);
        	accountManager.setMobile(mobile);
        	accountManager.setDesc(desc);
        	accountManager.setAddedTime(date);
        	accountManager.setAddedBy(user);
        	accountManager.setAccountGroup(accountGroupKey);
        }
       	if(accountManager == null){
       		accountManager = new AccountManager(name, gender, workEmail, tel, mobile, desc, accountGroupKey,  user,  new Date());
        }
        super.baseEdit(request, response, accountManager);
		response.getWriter().print(String.format(SUCCESS_MSG, "true","操作成功" ));
	}

	@Override
	public void del(HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		String id = request.getParameter("id");
		AccountManager accountManager = null;

        if(id!=null && !id.isEmpty()){
        	accountManager = pm.getObjectById(AccountManager.class, Long.parseLong(id));
        }
        System.out.println(accountManager);
        super.baseDel(request, response, accountManager);
		response.getWriter().print(String.format(SUCCESS_MSG, "true","操作成功" ));
		response.getWriter().print("{success: true}");
	}

	@Override
	public void search(HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		super.baseSearch(request, response, AccountManager.class);

	}

}
